package achievementbiz

import (
	"lab.com/consts"
	"lab.com/model"
	"lab.com/request/achievementrequest"
)

// AchievementList 科研成就列表
func AchievementList(params achievementrequest.AchievementListInputParams) (out achievementrequest.AchievementListRes, err error) {
	achModel := &model.Achievement{}

	var args []interface{}
	query := "is_show = ? and is_home = ? "
	args = append(args, consts.AchShow, params.IsHome)
	if params.IsHome == consts.AchNotInHome {
		query += "and title like ? "
		args = append(args, "%"+params.Title+"%")
	}
	if params.Type != 0 {
		query += "and type = ? "
		args = append(args, params.Type)
	}

	count, err := achModel.GlobalCount("*", query, args...)
	if err != nil {
		return out, err
	}
	info, err := achModel.GlobalFindPage("id, title, abstract, type, update_time, weight", "weight, id", params.Offset, params.Size, query, args...)
	if err != nil {
		return out, err
	}

	list := make([]achievementrequest.AchievementInfo, 0, len(info))

	for _, val := range info {
		list = append(list, achievementrequest.AchievementInfo{
			Id:         val.ID,
			Title:      val.Title,
			Abstract:   val.Abstract,
			Type:       val.Type,
			TypeDesc:   consts.AchievementTypeDesc[val.Type],
			Weight:     val.Weight,
			UpdateTime: val.UpdateTime.Format("2006-01-02 15:04:05"),
		})
	}

	out.List = list
	out.Count = int(count)
	return out, nil
}
